package com.gzw.dms.activity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.KJ.MyListView;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.OrderHistoryAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.BackOrderInfo;
import com.gzw.dms.bean.OrderHistoryInfo;
import com.gzw.dms.bean.SingnInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.DateUtil;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

public class BackFPDetailActivity extends BaseActivity {
    private TextView  tv_shiping_order_num, tv_custom_name, ic_time_limitation,ic_order_number,
            tv_custom_tel, tv_receipt_name, tv_receipt_tel, tv_goods_name, tv_goods_weight,
            tv_goods_vol, tv_shipping_order_state,tv_end_mechanism,ic_remarks_order,ic_receiptadress
            ,ic_staradress,ic_end_address,tv_send_mechanism,ic_remarks,ic_custom,ic_receipt,tv_goods_num,tv_goods_realnum;
    private Button btn_edit;
    private Intent mIntent;
    private BackOrderInfo shippingOrder;
    private Context mContext;
    private Toolbar toolbar;
    private List<String> ls_map;
    private ProgressDialog mProgressDialog;
    private String driver_id = "";
    private String driverName = "";
    private LinearLayout layout_receipt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_back_fpdetail);

    }

    @Override
    protected void initViews() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_shiping_order_num = (TextView) findViewById(R.id.ic_shiping_order_num).findViewById(R.id.tv_name);

        ic_staradress = (TextView) findViewById(R.id.ic_staradress).findViewById(R.id.tv_name);
        ic_end_address = (TextView) findViewById(R.id.ic_end_address).findViewById(R.id.tv_name);
        tv_send_mechanism = (TextView) findViewById(R.id.ic_send_mechanism).findViewById(R.id.tv_name);
        ic_order_number = (TextView) findViewById(R.id.ic_order_number).findViewById(R.id.tv_name);

        tv_custom_name = (TextView) findViewById(R.id.ic_custom_name).findViewById(R.id.tv_name);
        ic_custom = (TextView) findViewById(R.id.ic_custom).findViewById(R.id.tv_name);
        ic_time_limitation = (TextView) findViewById(R.id.ic_time_limitation).findViewById(R.id.tv_name);
        tv_custom_tel = (TextView) findViewById(R.id.ic_custom_tel).findViewById(R.id.tv_name);

        tv_receipt_name = (TextView) findViewById(R.id.ic_receipt_name).findViewById(R.id.tv_name);
        ic_receipt = (TextView) findViewById(R.id.ic_receipt).findViewById(R.id.tv_name);
        tv_receipt_tel = (TextView) findViewById(R.id.ic_receipt_tel).findViewById(R.id.tv_name);
        tv_end_mechanism = (TextView) findViewById(R.id.ic_end_mechanism).findViewById(R.id.tv_name);
        ic_receiptadress = (TextView) findViewById(R.id.ic_receipt_adress).findViewById(R.id.tv_name);

        tv_goods_name = (TextView) findViewById(R.id.ic_goods).findViewById(R.id.tv_left);
        tv_shipping_order_state = (TextView) findViewById(R.id.ic_goods).findViewById(R.id.tv_right);
        tv_goods_weight = (TextView) findViewById(R.id.ic_goodsWV).findViewById(R.id.tv_left);
        tv_goods_vol = (TextView) findViewById(R.id.ic_goodsWV).findViewById(R.id.tv_right);
        tv_goods_num = (TextView) findViewById(R.id.ic_goodsNum).findViewById(R.id.tv_left);
        tv_goods_realnum = (TextView) findViewById(R.id.ic_goodsNum).findViewById(R.id.tv_right);

        ic_remarks = (TextView) findViewById(R.id.ic_remarks).findViewById(R.id.tv_name);
        ic_remarks_order = (TextView) findViewById(R.id.ic_remarks_order).findViewById(R.id.tv_name);

        btn_edit = (Button) findViewById(R.id.btn_edit);
        layout_receipt = (LinearLayout)findViewById(R.id.layout_receipt);



    }
    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("运单详情");
        mIntent = getIntent();
        ls_map = new ArrayList<>();
        shippingOrder = (BackOrderInfo) mIntent.getExtras().get(
                "shippingOrder");
        driver_id=(String)mIntent.getExtras().get("driver_id");
        driverName=(String)mIntent.getExtras().get("driverName");
        setData(shippingOrder);
        setSupportActionBar(toolbar);

    }

    @Override
    protected void initEvents() {
        btn_edit.setOnClickListener(new mOnClick());
        toolbar.setNavigationOnClickListener(new mOnClick());

    }

    private void setData(BackOrderInfo shippingOrderInfo) {
        btn_edit.setText("分    配");
        tv_shiping_order_num.setText("货运编号:" + "    " + shippingOrderInfo.getShiping_order_num());
        ic_staradress.setText("起运位置:" + "    " + shippingOrderInfo.getSend_station());
        ic_end_address.setText("调拨/退货始发位置:" + "    " + shippingOrderInfo.getEnd_address());
        tv_send_mechanism.setText("受理机构:" + "    " + shippingOrderInfo.getSend_mechanism());
        ic_order_number.setText("出货订单号:" + "    " + shippingOrderInfo.getShiping_order_goid());


        tv_custom_name.setText("发货客户名称:" + "    " + shippingOrderInfo.getCustom_name());
        ic_custom.setText("发货联系人:" + "    " + shippingOrderInfo.getCustom_contact());
        ic_time_limitation.setText("提货时效:" + "    " + shippingOrderInfo.getTime_limitation());
        tv_custom_tel.setText("发货客户电话:" + "    " + shippingOrderInfo.getCustom_tel());

        tv_goods_name.setText("货物名称:" + "    " + shippingOrderInfo.getGoods_name());
        tv_goods_weight.setText("总重量:" + "    " + shippingOrderInfo.getGoods_weight());
        tv_goods_vol.setText("总体积:" + "    " + shippingOrderInfo.getGoods_vol());

        if (shippingOrderInfo.getOrder_type()==0){
            tv_shipping_order_state.setText("逆向物流类型:" + "    " +"退货");
            layout_receipt.setVisibility(View.GONE);
        }else {
            tv_shipping_order_state.setText("逆向物流类型:" + "    " + "调拨");
            layout_receipt.setVisibility(View.VISIBLE);
            tv_receipt_name.setText("收货客户名称:" + "    " + shippingOrderInfo.getReceipt_name());
            ic_receipt.setText("收货联系人:" + "    " + shippingOrderInfo.getReceipt_contact());
            tv_receipt_tel.setText("收货客户电话:" + "    " + shippingOrderInfo.getReceipt_tel());
            ic_receiptadress.setText("收货客户地址:" + "    " + shippingOrderInfo.getReceipt_address());
            tv_end_mechanism.setText("调拨/退货终到位置:" + "    " + shippingOrderInfo.getEnd_mechanism());
        }
        tv_goods_num.setText("总件数:" + "    " + shippingOrderInfo.getGoods_num() );
        tv_goods_realnum.setText("实际件数:" + "    " + shippingOrderInfo.getReal_num());
        UserInfo userInfo = MyApplication.getUserInfo();
        String flag = userInfo.getFlag();
        if (flag.equals("3")||flag.equals("4")){
            ic_remarks_order.setText("项目部备注:" + "    " + shippingOrderInfo.getRemarks_order());
            ic_remarks.setVisibility(View.GONE);
        }else {
            ic_remarks.setText("供应商/司机备注:" + "    " + shippingOrderInfo.getRemarks());
            ic_remarks_order.setVisibility(View.GONE);

        }

    }
    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_edit:
                    ls_map.add(shippingOrder.getShiping_order_id());
                    ReceiveOrderTask fpOrderTask = new ReceiveOrderTask();
                    fpOrderTask.execute();
                    break;

                default:
                    finish();
                    break;
            }
        }
    }

    public class ReceiveOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(String... params) {
            String orders=ls_map.toString().substring(1,ls_map.toString().length()-1);
            String str1 = orders.replace(" ", "");
            Map<String, String> map = new HashMap<>();
            map.put("driverId",driver_id);
            map.put("driverName",driverName);
            map.put("orders",str1);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appSaveDistributionReturnGoods"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            mProgressDialog.dismiss();

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else if (result.indexOf("successMsg") > 0){
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(1, mIntent);
                    finish();
                }
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }
}
